﻿using System;
using System.Collections;
using LuaFramework;
using UnityEngine;

namespace LuaFramework
{
    public class SceneManager
    {

        private static string _curSceneName;

        public static void ChangeScene(string sceneName, bool isAsync, Action callback)
        {
            _curSceneName = sceneName;
            if (isAsync)
            {
                Main.Inst.StartCoroutine(ChangeSceneAsync(sceneName, callback));
            }
            else
            {
                UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
                Main.Inst.StartCoroutine(OnChangeScene(callback));
            }
        }

        private static IEnumerator ChangeSceneAsync(string sceneName, Action callback)
        {
            yield return UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName);
            yield return OnChangeScene(callback);
        }

        private static IEnumerator OnChangeScene(Action callback)
        {
            yield return null;
            if (callback != null)
            {
                callback.Invoke();
            }
        }

        public static string GetCurSceneName()
        {
            if (string.IsNullOrEmpty(_curSceneName))
            {
                return UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
            }
            return _curSceneName;
        }
    }
}